package dao;

import bean.GitVersionInfo;
import bean.StudentJob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import util.Util;

import java.sql.SQLException;
import java.sql.Timestamp;

public class GitVersionInfoDao {
    public static void saveVersion(StudentJob studentJob,int jobId){
        Connection conn = Util.getConn();
        int versions=studentJob.getGitVersionInfos().size();
        System.out.println(versions);
        String sql = "insert into commitInfo(jobid,name,time,version) values(?,?,?,?)";
        PreparedStatement pstmt=null;
            try {
                for(int k=0;k<versions;k++) {
                    GitVersionInfo gitVersionInfo = studentJob.getGitVersionInfos().get(k);
                    pstmt = (PreparedStatement) conn.prepareStatement(sql);
                    pstmt.setInt(1, jobId);
                    pstmt.setString(2, gitVersionInfo.getName());
                    Timestamp t = new Timestamp(gitVersionInfo.getDate().getTime());
                    pstmt.setTimestamp(3, t);
                    pstmt.setString(4, gitVersionInfo.getVersion());
                    pstmt.executeUpdate();
                }
                if (pstmt!=null) pstmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
}
